﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SD.IBUS;
using SD.Entities;
using SD.BUS;

namespace SD.UI
{
    public partial class SearchAddress : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void btnSearchAddress_Click(object sender, EventArgs e)
        {
            IAddressBIZ iAddress = new AddressBIZ();
            DataTable dt = iAddress.ListAddressByCodition(tbxPostcode.Text.ToString(), tbxStreet.Text.ToString(), tbxTown.Text.ToString());

            gvSearchAddress.DataSource = dt;
            gvSearchAddress.DataBind();

            // If have data in table
            if (dt.Rows.Count > 0)
            {
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                lblMessage.Text = string.Empty;
            }
            else // if have no data in table
            {
                dt.Rows.Add(dt.NewRow());
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                gvSearchAddress.Rows[0].Visible = false;
                lblMessage.Text = "No records found";
            }
        }

        protected void btnNone_Click(object sender, EventArgs e)
        {

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            tbxPostcode.Text = "";
            tbxStreet.Text = "";
            tbxTown.Text = "";
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string selectValue = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[2].Text;//Request.Form["rdbChoose"];
            string contactValue = gvSearchAddress.SelectedValue.ToString();//Request.Form["contactID"];  
            IAddressBIZ addressBIZ = new AddressBIZ();
            Address address = addressBIZ.GetById(contactValue);
            string line2 = address.Line2;
            string line3 = address.Line3;
            string address1 = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[1].Text;
            string town = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[3].Text;
            string county = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[4].Text;
            string country = gvSearchAddress.Rows[gvSearchAddress.SelectedIndex].Cells[5].Text;
            string str = "var Str = '" + selectValue + "';var ctID='" + contactValue + "';var vAddress='"
                + address1 + "';var vTown='" + town + "';var vCountry='" + country + "';var vCounty='"
                + county + "';var vLine2='" + line2 + "';var vLine3='" + line3 +
                "';window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxAddressLine2').value=vLine2;"+
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxAddressLine3').value=vLine3;" +
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxCounty').value=vCounty;"+
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxNationCountry').value=vCountry;" +
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxAddressLine1').value=vAddress;"+
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxCityTown').value=vTown;" +
                " window.opener.document.getElementById('ctl00_ContentPlaceHolder1_TabContainer1_tabsDetail1_tbxPostcode').value=Str;"+
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_txtAddressID').value=ctID;" +
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfLine1').value=vAddress;" +
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfLine2').value=vLine2;" +
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfLine3').value=vLine3;" +
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfPostCode').value=Str;" +
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfTown').value=vTown;" +
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfCounty').value=vCounty;" +
                "window.opener.document.getElementById('ctl00_ContentPlaceHolder1_hdfCountry').value=vCountry;" + 
                "window.close();";
            ScriptManager.RegisterClientScriptBlock(btnSelect, typeof(Button), "select", str, true);
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {

        }

        private void LoadData()
        {
            IAddressBIZ iAddress = new AddressBIZ();
            DataTable dt = iAddress.ListAddress();

            // If have data in table
            if (dt.Rows.Count > 0)
            {
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                lblMessage.Text = string.Empty;
            }
            else // if have no data in table
            {
                dt.Rows.Add(dt.NewRow());
                gvSearchAddress.DataSource = dt;
                gvSearchAddress.DataBind();
                gvSearchAddress.Rows[0].Visible = false;
                lblMessage.Text = "No records found";
            }
        }
        protected void rdbChoose_CheckedChanged(object sender, EventArgs e)
        {

            Deselect();
            RadioButton rb = (RadioButton)sender;
            rb.Checked = true;
            GridViewRow row = (GridViewRow)((RadioButton)sender).NamingContainer;

            if (row != null)
            {
                gvSearchAddress.SelectedIndex = row.RowIndex;
            }
        }
        private void Deselect()
        {
            //GridViewRow row = new GridViewRow();
            int i = 0;
            foreach (GridViewRow r in gvSearchAddress.Rows)
            {
                RadioButton rb = (RadioButton)(gvSearchAddress.Rows[i].FindControl("rdbChoose"));
                rb.Checked = false;
                i++;

            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            btnCreate.Attributes.Add("onclick", "window.open('AddAddress.aspx')");
        }
    }
}
